class Admin::VariantsController < Admin::AdminController
  
  filter_resource_access

  before_filter :load_product
  
  def index
    @search   = @product.variants_with_master.search(params[:search])
  end

  def create
    if @product.variants.new(params[:variant]).save
      redirect_to admin_product_variants_path(:product_id => @product.id), :notice => "Varianta byla úspěšně uložena."
    else
      render :action => :new
    end
  end

  def edit
    @variant = Variant.find(params[:id])
    
    respond_to do |format|
      format.html
      format.js  {render :action => "edit", :layout => false}
    end
  end

  def update
    if @variant.update_attributes(params[:variant])
      redirect_to admin_product_variants_path(:product_id => params[:product_id]), :notice => "Varianta byla úspěšně upravena."
    else
      render :action => :edit
    end
  end

  def new
    respond_to do |format|
      format.html
      format.js   {render :action => "new", :layout => false}
    end
  end

  def destroy
    @variant = Variant.find(params[:id])

    if @variant.destroy
      flash[:notice] = "Varianta byla úspěšně smazána. #{undo_link(@variant)}"
    else
      flash[:error]  = "Variantu se nepodařilo smazat."
    end

    redirect_to admin_variants_path(params[:id])
  end

  private
  def load_product
    @product  = Product.find(params[:product_id] || params[:id])
  end
end
